Load Libraries

library(tidyverse)
library(tidycensus)
library(leaflet)
library(sf)
library(stringr)

Set API key

Insert your own API Key. You may request a free key from the Census API tool https://api.census.gov/data/key_signup.html

#census_api_key("did you enter your API key here?")

Choose a census variable

I recommend using one of the following variables. However, you can use the tidycensus::load_variables() and follow the “Searching for variables” instructions.

  • B08103_001E - MEDIAN AGE BY MEANS OF TRANSPORTATION TO WORK: Total: Taxicab, motorcycle, bicycle, or other means: Workers 16 years and over – (Estimate)
  • B08131_001E - AGGREGATE TRAVEL TIME TO WORK (IN MINUTES) OF WORKERS BY PLACE OF WORK–STATE AND COUNTY LEVEL: Worked in State of residence: Workers 16 years and over who did not work at home – (Estimate)
  • B19013_001E - median household income

Median Age by Means of Transportation to Work

  • B08103_001E

get_acs()

Load the variable and assign and object name for some USA county using the get_acs function.

census_variable <- 
  get_acs(geography = "county",
          variables = "B08103_001E",
          state = "NC",
          geometry = TRUE)

census_variable

Make choropleth in Leaflet

Generate a Palette

Using the estimate variable in the census_variable sf object, generate a MapPalette.

MapPalette <- colorBin(palette = "viridis",
                       domain = census_variable$estimate,
                       bins = 4,
                       pretty = TRUE,
                       reverse = TRUE)

Make Leaflet choropleth

Make choropleth by filling county polygons (census geography) with correlated value (from the ACS)

census_variable %>%
    st_transform(crs = "+init=epsg:4326") %>%
    leaflet(width = "100%") %>%
    addProviderTiles(provider = "CartoDB.Positron") %>%
    addPolygons(popup = ~ str_extract(NAME, "^([^,]*)"),
                stroke = FALSE,
                smoothFactor = 0,
                fillOpacity = 0.7,
                color = ~ MapPalette(estimate)) %>%
    addLegend("bottomright", 
              pal = MapPalette, 
              values = ~ estimate,
              title = "Median Age of<br>Travelers by<br>Taxi/Moto/Bike",
              opacity = 1)
<<<<<<< HEAD
=======
>>>>>>> 816cf5afbdf428a2c4ceaf2eaee2b15c5882faad

Travel Time

  • B08131_001E

AGGREGATE TRAVEL TIME TO WORK (IN MINUTES)

census_variable <- 
  get_acs(geography = "county",
          variables = "B08131_001E",
          state = "NC",
          geometry = TRUE)
## Getting data from the 2012-2016 5-year ACS
## Downloading feature geometry from the Census website.  To cache shapefiles for use in future sessions, set `options(tigris_use_cache = TRUE)`.
census_variable

AGGREGATE TRAVEL TIME TO WORK (IN MINUTES)

Convert Minutes to hours, hours to months.

census_variable <-  census_variable %>% 
  rename(estimate_original = estimate) %>% 
  mutate(estimate = estimate_original / 60 / 730.0008)
MapPalette <- colorBin(palette = "viridis",
                       domain = census_variable$estimate,
                       bins = 7,
                       pretty = TRUE,
                       reverse = TRUE)
census_variable %>%
    st_transform(crs = "+init=epsg:4326") %>%
    leaflet(width = "100%") %>%
    addProviderTiles(provider = "CartoDB.Positron") %>%
    addPolygons(popup = ~ str_extract(NAME, "^([^,]*)"),
                stroke = FALSE,
                smoothFactor = 0,
                fillOpacity = 0.7,
                color = ~ MapPalette(estimate)) %>%
    addLegend("bottomright", 
              pal = MapPalette, 
              values = ~ estimate,
              title = "Agregate Travel Time<br>in Months",
              opacity = 1)
<<<<<<< HEAD
=======
>>>>>>> 816cf5afbdf428a2c4ceaf2eaee2b15c5882faad

Median Household Income in the Past 12 Months

  • B19013_001E
census_variable <- 
  get_acs(geography = "county",
          variables = "B19013_001E",
          state = "NC",
          geometry = TRUE)
## Getting data from the 2012-2016 5-year ACS
## Downloading feature geometry from the Census website.  To cache shapefiles for use in future sessions, set `options(tigris_use_cache = TRUE)`.
census_variable
MapPalette <- colorNumeric(palette = "viridis",
                           domain = census_variable$estimate,
                           reverse = TRUE)
census_variable %>%
    st_transform(crs = "+init=epsg:4326") %>%
    leaflet(width = "100%") %>%
    addProviderTiles(provider = "CartoDB.Positron") %>%
    addPolygons(popup = ~ str_extract(NAME, "^([^,]*)"),
                stroke = FALSE,
                smoothFactor = 0,
                fillOpacity = 0.7,
                color = ~ MapPalette(estimate)) %>%
    addLegend("bottomright", 
              pal = MapPalette, 
              values = ~ estimate,
              title = "Median Household Income",
              labFormat = labelFormat(prefix = "$"),
              opacity = 1)
<<<<<<< HEAD
=======
>>>>>>> 816cf5afbdf428a2c4ceaf2eaee2b15c5882faad
 
R We Having Fun Yet‽ -- Learning Series
Data & Visualization Services
Duke University Libraries
C bn
Shareable via Creative Commons: CC By-NC